/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package controllers

import (
	"encoding/json"
	"prim-server/common"
	"prim-server/errcode"
	"prim-server/logger"
	"prim-server/primservice/validator"
	service "prim-server/service"
	orderService "prim-server/service/order"
	userService "prim-server/service/user"
)

const (
	QueryOnePayOrderURL   = "/v1/user/order/query/onePay"
	QueryOnDemandOrderURL = "/v1/user/order/query/onDemand"
	CancelOnePayOrderURL  = "/v1/user/order/cancel"
	PayOnePayOrderURL     = "/v1/user/order/pay"

	//测试接口
	PullOrderURL = "/v1/order/pull"
)

type OrderController struct {
	CommonController
}

// 拉取订单测试--测试，无需校验
func (oc *OrderController) PullOrder() {
	userId := oc.Ctx.Input.Query("userId")
	cloudUser, err := userService.GetCloudUserOne(userId)
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithErr(err))
		return
	}
	err = orderService.PullAndSaveCloudOnePayOrderLastestOne(userId, "", cloudUser.CloudId)
	if err != nil {
		oc.HandleErrCode(errcode.E.Internal.Internal.WithErr(err))
		return
	}
	err = orderService.PullAndSaveOnDemandResouceLastestOne(userId, "", cloudUser.CloudId)
	if err != nil {
		oc.HandleErrCode(errcode.E.Internal.Internal.WithErr(err))
		return
	}
	oc.HandleBusinessSuccess()
}

// QueryOnPayOrder  普通用户查询包周期订单信息表
func (oc *OrderController) QueryOnPayOrder() {
	req := common.OrderOnePayQueryRequest{}
	if err := json.Unmarshal(oc.Ctx.Input.RequestBody, &req); err != nil {
		logger.Error("QueryOnPayOrder Unmarshal error: %v", err)
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	if err := validator.Validate(&req); err != nil {
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	err := oc.CheckUserIdFromSession(req.UserId)
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	//管理员不可调用该接口
	adminId, err := userService.GetAdminId()
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	if adminId == req.UserId {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("非法请求"))
		return
	}
	response, err := orderService.BuildResponseForQueryOnePayOrder(&req)
	if err != nil {
		oc.HandleErrCode(errcode.E.Internal.Internal.WithErr(err))
		return
	}
	oc.HandleBusinessObj(response)
}

// QueryOnDemandOrder  普通用户查询按需订单信息表
func (oc *OrderController) QueryOnDemandOrder() {
	req := common.OrderOnDemandQueryRequest{}
	if err := json.Unmarshal(oc.Ctx.Input.RequestBody, &req); err != nil {
		logger.Error("QueryOnDemandOrder Unmarshal error: %v", err)
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	if err := validator.Validate(&req); err != nil {
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	err := oc.CheckUserIdFromSession(req.UserId)
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	//管理员不可调用该接口
	adminId, err := userService.GetAdminId()
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	if adminId == req.UserId {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("非法请求"))
		return
	}
	response, err := orderService.BuildResponseForQueryOnDemandOrder(&req)
	if err != nil {
		oc.HandleErrCode(errcode.E.Internal.Internal.WithErr(err))
		return
	}
	oc.HandleBusinessObj(response)
}

// CancelOnePayOrder 取消包周期订单
func (oc *OrderController) CancelOnePayOrder() {
	req := common.CancelOnePayOrderRequest{}
	if err := json.Unmarshal(oc.Ctx.Input.RequestBody, &req); err != nil {
		logger.Error("CancelOnePayOrder Unmarshal error: %v", err)
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	if err := validator.Validate(&req); err != nil {
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	if err := oc.CheckUserIdFromSession(req.UserId); err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	//管理员不可调用该接口
	adminId, err := userService.GetAdminId()
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	if adminId == req.UserId {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("非法请求"))
		return
	}
	//获取订单
	order, err := orderService.GetOnePayOrderWithOrderId(req.OrderId)
	if order == nil || err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("订单不存在"))
		return
	}
	if order.UserId != req.UserId {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("非法请求"))
		return
	}
	err = orderService.CancelCustomerOnePayOrder(order)
	if err != nil {
		oc.HandleErrCode(errcode.E.Internal.Internal.WithErr(err))
		return
	}
	oc.HandleBusinessSuccess()
}

// PayOnePayOrder 支付包周期订单
func (oc *OrderController) PayOnePayOrder() {
	req := common.PayOnePayOrderRequest{}
	if err := json.Unmarshal(oc.Ctx.Input.RequestBody, &req); err != nil {
		logger.Error("CancelOnePayOrder Unmarshal error: %v", err)
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	if err := validator.Validate(&req); err != nil {
		oc.HandleErrCode(errcode.E.Base.RequestInvalid.WithErr(err))
		return
	}
	if err := oc.CheckUserIdFromSession(req.UserId); err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	//管理员不可调用该接口
	adminId, err := userService.GetAdminId()
	if err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("用户检验失败"))
		return
	}
	if adminId == req.UserId {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("非法请求"))
		return
	}
	//获取订单
	order, err := orderService.GetOnePayOrderWithOrderId(req.OrderId)
	if order == nil || err != nil {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("订单不存在"))
		return
	}
	if order.UserId != req.UserId {
		oc.HandleErrCode(errcode.E.Base.ErrorMessage.WithMessage("非法请求"))
		return
	}
	err = service.PayOnePayOrderEntrance(order)
	if err != nil {
		oc.HandleErrCode(errcode.E.Internal.Internal.WithErr(err))
		return
	}
	oc.HandleBusinessSuccess()
}
